<template>
  <el-breadcrumb :separator-icon="ArrowRight">
    <el-breadcrumb-item :to="{ path: '/' }">首页(登录)</el-breadcrumb-item>
    <el-breadcrumb-item>门诊</el-breadcrumb-item>
    <el-breadcrumb-item>门诊充值记录</el-breadcrumb-item>
  </el-breadcrumb>

  <div>
    <!-- 标题和工具栏 -->
    <el-card style="margin-top: 10px;">
      <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px;">
        <div style="display: flex; align-items: center;">
          <el-icon :size="20" style="margin-right: 8px; color: #409eff;">
            <Wallet />
          </el-icon>
          <span style="font-size: 18px; font-weight: 600; color: #303133;">门诊充值记录</span>
        </div>
        <div>
          <el-button type="primary" plain>导入</el-button>
          <el-button type="primary">导出</el-button>
        </div>
      </div>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="search" class="demo-form-inline">
        <el-form-item>
          <el-date-picker
            v-model="search.dateRange"
            type="daterange"
            range-separator="~"
            start-placeholder="请选择日期"
            end-placeholder="请选择日期"
            value-format="YYYY-MM-DD"
            unlink-panels
            clearable
            style="width: 340px;"
          />
        </el-form-item>
        <el-form-item>
          <el-input 
            v-model="search.keyword" 
            placeholder="请输入病人姓名/住院号" 
            clearable 
            style="width: 240px;" 
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 列表 -->
    <el-card style="margin-top: 10px;">
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="operatingTime" label="操作时间" width="180" align="center">
          <template #default="{ row }">
            {{ row.operatingTime ? moment(row.operatingTime).format('YYYY-MM-DD HH:mm:ss') : '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="operatingNum" label="订单编号" width="160" align="center" />
        <el-table-column prop="name" label="患者姓名" width="100" align="center" />
        <el-table-column prop="medicalCard" label="就诊卡号" width="120" align="center" />
        <el-table-column prop="source" label="来源" width="120" align="center" />
        <el-table-column prop="businessName" label="业务类型" width="110" align="center" />
        <el-table-column prop="paymentMethod" label="支付方式" width="100" align="center" />
        <el-table-column prop="paymentAmount" label="支付金额（元）" width="120" align="center">
          <template #default="{ row }">
            <span style="color: #f56c6c; font-weight: 500;">{{ row.paymentAmount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="paymentStatus" label="支付状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.paymentStatus === '已支付'">已支付</el-tag>
            <el-tag type="warning" v-else-if="row.paymentStatus === '待支付'">待支付</el-tag>
            <el-tag type="info" v-else>未支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="balance" label="卡内余额（元）" width="130" align="center">
          <template #default="{ row }">
            <span style="color: #67c23a; font-weight: 500;">{{ row.balance }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center" fixed="right">
          <template #default>
            <el-button type="primary" :icon="Document" circle />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
     <div>
        <el-pagination
          v-model:current-page="page.PageIndex"
          v-model:page-size="page.PageSize"
          :page-sizes="[2, 4, 6]"
          layout="prev, pager, next, sizes, jumper"
          :total="page.totalCount"
          class="pagination"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, watch } from 'vue'
import { ArrowRight, Wallet, Document } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { dataRead } from '@/Utils/Http'
import moment from 'moment'

interface PageState {
  PageIndex: number
  PageSize: number
  totalCount: number
  pageCount: number
}

interface SearchForm {
  dateRange: string[]
  keyword: string
}

const loading = ref(false)
const jumpPage = ref('')

const page = reactive<PageState>({ 
  PageIndex: 1, 
  PageSize: 2, 
  totalCount: 0,
  pageCount: 0
})

const search = reactive<SearchForm>({
  dateRange: [],
  keyword: ''
})

const tableData = ref([])

// 搜索
const handleSearch = () => {
  page.PageIndex = 1
  getTableData()
}

// 分页大小改变
const handleSizeChange = (val: number) => {
  page.PageSize = val
  page.PageIndex = 1
  getTableData()
}

// 当前页改变
const handleCurrentChange = (val: number) => {
  page.PageIndex = val
  getTableData()
}

// 跳转页面
const handleJumpPage = () => {
  const pageNum = parseInt(jumpPage.value)
  if (pageNum > 0 && pageNum <= page.pageCount) {
    page.PageIndex = pageNum
    getTableData()
    jumpPage.value = ''
  } else {
    ElMessage.warning('请输入有效的页码')
  }
}

//侦听器

watch(page,()=>{
  getTableData()
})
// 获取表格数据
const getTableData = () => {
  dataRead.get('/BaseData/AmountQuery?BusinessId=2', { 
    params:{
        PageIndex: page.PageIndex,
        PageSize: page.PageSize,
    }
   })
    .then((res) => {
      if (res.data.code === 200) {
        tableData.value = res.data.data.data
        page.totalCount = res.data.data.totalCount
        page.pageCount = res.data.data.pageCount
      }
    })
    .catch(() => {
      ElMessage.error('获取数据失败')
    })
    .finally(() => {
      loading.value = false
    })
}

onMounted(() => {
  getTableData()
})
</script>

<style scoped>
.demo-form-inline {
  margin-top: 8px;
}

:deep(.el-card) {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th.el-table__cell) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}

:deep(.el-table .el-table__row:hover) {
  background-color: #f5f7fa;
}

:deep(.el-pagination) {
  font-weight: 400;
}

:deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
  background-color: #409eff;
  color: #fff;
}
</style>

